<?php
namespace app\index\controller;

class Coupon extends Common
{
    //领券中心页面
    public function index()
    {
        $user_id = session('id');
        $coupon_list = db('coupon')->where("status=1")->order('id asc')->select();
        $user_coupon_list = db('coupon_user')->where("user_id={$user_id}")->order('id asc')->select();
        $hasCoupon_ids = [];
        foreach ($user_coupon_list as $key => $value) {
            $hasCoupon_ids[$key] = $value['coupon_id'];
        }
        // dump($hasCoupon_ids);die;
        $this->assign('coupon_list',$coupon_list);
        $this->assign('hasCoupon_ids',$hasCoupon_ids);
        return view();
    }
    //Ajax领取优惠券功能
    public function getCoupon(){
        $user_id = session('id');
        $coupon_id = input('post.coupon_id');
        $data = [
            'user_id' => $user_id,
            'coupon_id' => $coupon_id,
        ];
        $ret = db('coupon_user')->insert($data);
        if($ret==FALSE){
            $this->error("领取失败!",'',1);
        }else{
            $this->success("领取成功",'',2);
        }
    }
}